package com.dieusoft.box.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dieusoft.box.base.mapper.JzxBaseCommonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;

@Api(tags = "公共信息")
@RestController
@RequestMapping("/box.common/")
@Slf4j
public class BaseController {

    @Resource
    private JzxBaseCommonMapper jzxBaseCommonMapper;

    //封装了redis操作各种方法
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 根据主单号(提单号)获取箱子信息
     * TODO 以捷丰为例
     *
     * @param blno 主单号(提单号)
     * @return
     */
    @GetMapping(value = "/dynamic/boxinfo")
    public Result getBoxDynamicInfo(@RequestParam(name = "blno") String blno) {
        try {
            Document doc = Jsoup.parse(new URL("http://www.sdsmartlogistics.com/FindClass/YD_BLQueryGrid.aspx?blno=" + blno), 10000);
//            log.info(doc.toString());
            // 根据id获取table
            Element table = doc.getElementById("Grid3");
            // 使用选择器选择该table内所有的<tr> <tr/>
            Elements trs = table.select("tr");
            JSONArray boxArray = new JSONArray();
            for (int i = 1; i < trs.size(); ++i) {
                if (i + 1 == trs.size()) {
                    break;
                }
                // 获取一个tr
                Element tr = trs.get(i);
                // 获取该行的所有td节点
                Elements tds = tr.select("td");
                JSONObject boxInfo = new JSONObject();
                // 选择某一个td节点
                for (int j = 1; j < tds.size(); ++j) {
                    Element td = tds.get(j);
                    Node node = td.childNodes().get(0);
                    switch (j) {
                        case 1:
                            boxInfo.put("boxnum", node.toString());
                            break;
                        case 2:
                            boxInfo.put("boxtype", tds.get(j + 1).childNodes().get(0) + node.toString());
                            break;
                        case 4:
                            boxInfo.put("title", node.toString());
                            break;
                        case 5:
                            boxInfo.put("js", node.toString());
                            break;
                        case 6:
                            boxInfo.put("jsbz", node.toString());
                            break;
                        case 7:
                            boxInfo.put("weight", node.toString());
                            break;
                        case 8:
                            boxInfo.put("cm", node.toString());
                            break;
                    }
//                    // 获取td节点的所有div
//                    Elements divs = td.select("div");
//                    // 选择一个div
//                    for (int k = 0; k < divs.size(); k++) {
//                        Element div = divs.get(k);
//                        //获取文本信息
//                        String text = div.text();
//                        //输出到控制台
//                        log.info(text);
//                    }
                }
                boxArray.add(boxInfo);
            }
            log.info(boxArray.toJSONString());
            return Result.OK(boxArray);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("信息获取超时请重试");
        }
    }

    /**
     * 添加新客户公司抬头
     * TODO 客户取消在下拉列表中可以增加客户的需求
     *
     * @param json
     * @return
     */
    @AutoLog(value = "新客户公司-快速添加")
    @ApiOperation(value = "新客户公司-快速添加", notes = "新客户公司-快速添加")
    @PostMapping(value = "/add")
    @Deprecated
    public Result<?> addNewCustomer(@RequestBody JSONObject json) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Object customerTitle = json.get("customerTitle");
        String idStr = IdWorker.getIdStr();
//        redisUtil.hset()
        jzxBaseCommonMapper.fastSaveCustomer(customerTitle.toString(), idStr, sysUser.getUsername(), LocalDateTime.now().toString(), sysUser.getOrgCode());
        return Result.OK("添加成功！", idStr);
    }

    /**
     * 添加新供应商公司抬头
     * TODO 客户取消在下拉列表中可以增加客户的需求
     *
     * @param json
     * @return
     */
    @AutoLog(value = "新供应商-快速添加")
    @ApiOperation(value = "新供应商-快速添加", notes = "新供应商-快速添加")
    @PostMapping(value = "/add/foreignAgent")
    @Deprecated
    public Result<?> addNewForeignAgentCustomer(@RequestBody JSONObject json) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Object customerTitle = json.get("customerTitle");
        String idStr = IdWorker.getIdStr();
        jzxBaseCommonMapper.fastSaveForeignAgentCustomer(customerTitle.toString(), idStr, sysUser.getUsername(), LocalDateTime.now().toString(), sysUser.getOrgCode());
        return Result.OK("添加成功！", idStr);
    }
}
